package com.bw.mallshop.mvvm.viewmodel;

import androidx.databinding.ObservableField;

import com.bw.mallshop.MainActivity;
import com.bw.mallshop.mvvm.model.SplashModel;
import com.bw.mallshop.mvvm.model.entity.TokenEntity;
import com.bw.mallshop.timmer.TimeHandler;
import com.bw.mallshop.timmer.TimeThread;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.field.UIField;
import com.bw.mvvm_common.mvvm.utils.SPUtils;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/13 0013 下午 6:30
 *
 */
class SplashViewModel extends BaseViewModel<SplashModel> {
    public ObservableField<String> timeObserable = new ObservableField<>();
    private TimeHandler handler;

    @Override
    public void onCreate() {
        super.onCreate();
        requestToken();
        handler=new TimeHandler(this);
    }

    private void requestToken() {
        HashMap<String,Object> map = new HashMap<>();
        map.put("imie",""+System.currentTimeMillis());
        m.requestToken(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected void result(BaseEntity entity) {
        if (entity.getMessage().equals("imie为空")){
            showMsg(entity.getMessage());return;
        }
        if (entity.getMessage().equals("Sign为空")){
            showMsg(entity.getMessage());return;
        }
        TokenEntity tokenEntity = (TokenEntity) entity;
        //存入sp
        SPUtils.writeToken(tokenEntity.getValues().token);
        //启动定时线程
        new TimeThread(handler).start();
    }

    public void skipTimmer(){
        HashMap<String,Object>map = new HashMap<>();
        map.put(UIField.PAGECLAZZ, MainActivity.class);
        startPage(map);
    }
}
